package com.me.brainlessbirds.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.utils.Timer.Task;
import com.me.brainlessbirds.actors.interfaces.Moveable;
import com.me.brainlessbirds.actors.zombies.units.Smoothie;

/**
 * @author Maxens (for comment)
 *         Unit can Attack, move, blocked, be selected
 *         Unit can collide
 */

public abstract class Unit extends GameObject implements Moveable
{
	protected float				attack		= 0.0f;
	protected float				defense		= 0.0f;
	protected boolean			moving		= true;
	protected boolean			blocked		= false;
	protected boolean			selected	= false;
	protected float				rotation	= 1;					// direction : 1 = droite, -1 = gauche
	private float				maxRotation	= 5;
	protected boolean			bumped		= false;
	protected SequenceAction	sequence;
	protected TextureRegion		rgnIdle		= new TextureRegion();
	protected TextureRegion		rgnAtk		= new TextureRegion();

	@Override
	public void draw(SpriteBatch batch, float parentAlpha)
	{
		batch.setColor(this.getColor());
		if (this.moving && !(this instanceof Smoothie))
		{
			if (!this.rgn.equals(this.rgnIdle))
			{
				this.rgn = this.rgnIdle;
			}
			if (this.rotation > 0)
			{
				setRotation(getRotation() + Gdx.graphics.getDeltaTime() * this.speed);
				if (getRotation() > this.maxRotation)
				{
					this.rotation = -1;
				}
			}
			else if (this.rotation < 0)
			{
				setRotation(getRotation() - Gdx.graphics.getDeltaTime() * this.speed);
				if (getRotation() < -this.maxRotation)
				{
					this.rotation = 1;
				}
			}
		}
		else
		{
			this.rotation = 1;
			setRotation(0);
		}
		batch.draw(rgn, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
	}

	public float getAttack()
	{
		return this.attack;
	}

	public boolean getMoving()
	{
		return this.moving;
	}

	public void setMoving(boolean mov)
	{
		this.moving = mov;
	}

	public void destroy(Task callbackTask)
	{
		super.destroy(callbackTask);
	}

	public SequenceAction getSequence()
	{
		return this.sequence;
	}

	public void stopBecauseCollide()
	{
		this.clearActions();
		this.setMoving(false);
	}

	public void setBlocked(boolean blocked)
	{
		this.blocked = blocked;
	}

	public void setSelected(boolean select)
	{
		this.selected = select;
	}

	@Override
	public void addMoveToLeft(Stage stage, Task _callbackTask)
	{
		if (this.blocked)
		{
			this.moving = false;
			this.stopBecauseCollide();
			return;
		}
		float distance = getX();
		float totalDuration = distance / this.speed;
		sequence = Actions.sequence(Actions.moveTo(0f, getY(), totalDuration), Actions.run(_callbackTask));
		addAction(sequence);
		this.moving = true;

	}

	@Override
	public void addMoveToRight(Stage stage, Task _callbackTask)
	{
		if (this.blocked || this.selected)
		{
			this.moving = false;
			this.stopBecauseCollide();
			return;
		}
		float distance = (stage.getWidth() + getWidth()) - getX();
		float totalDuration = distance / this.speed;

		sequence = Actions.sequence(Actions.moveTo(stage.getWidth() + getWidth(), getY(), totalDuration), Actions.run(_callbackTask));

		addAction(sequence);
		this.moving = true;
	}

	public void addMoveToTop(Stage stage, Task _callbackTask)
	{
		sequence = Actions.sequence(Actions.moveTo(getX(), stage.getHeight() + getHeight(), 1f), Actions.run(_callbackTask));
		addAction(sequence);
	}

	public boolean isBumped()
	{
		return this.bumped;
	}
}
